package com.telchina.workorder.third.service.impl;


import cn.hutool.db.PageResult;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.telchina.common.core.domain.AjaxResult;
import com.telchina.workorder.third.config.BpmConfig;
import com.telchina.workorder.third.service.BpmProcessInstanceThirdService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * 流程实例 Service 实现类
 * <p>
 * ProcessDefinition & ProcessInstance & Execution & Task 的关系：
 * 1. https://blog.csdn.net/bobozai86/article/details/105210414
 * <p>
 * HistoricProcessInstance & ProcessInstance 的关系：
 * 1.https://my.oschina.net/843294669/blog/719024
 * 简单来说，前者 = 历史 + 运行中的流程实例，后者仅是运行中的流程实例
 *
 * @author telchina
 */
@Service
@Validated
@Slf4j
public class BpmProcessInstanceThirdServiceImpl implements BpmProcessInstanceThirdService {

    @Autowired
    private BpmConfig bpmConfig;

    /**
     * 远程流程调用
     *
     * @param paramsMap
     * @return
     */
    @Override
    public AjaxResult createProcessInstance(Map<String, Object> paramsMap) {
        log.info("开始调用远程服务{},参数为：{}", "createProcessInstance", paramsMap);
        // 获取请求的地址
        String sendUrl = bpmConfig.getBaseUrl() + bpmConfig.getCreateProcessInstanceApi();
        String postResult = HttpUtil.post(sendUrl, JSON.toJSONString(paramsMap));
        log.info("结束调用远程服务{},返回结果为：{}", "createProcessInstance", postResult);
        AjaxResult ajaxResult = JSON.parseObject(postResult, AjaxResult.class);
        return ajaxResult;
    }

    /**
     * 获取流程图  高亮显示流程节点
     *
     * @param processInstanceId
     */
    @Override
    public AjaxResult generateHighlightDiagram(String processInstanceId) {
        log.info("开始调用远程服务{},参数为：{}", "generateHighlightDiagram", processInstanceId);
        // 获取请求的地址
        String sendUrl = bpmConfig.getBaseUrl() + bpmConfig.getGenerateHighlightDiagramApi();
        return AjaxResult.success("", HttpUtil.get(sendUrl+ "?processInstanceId=" + processInstanceId))  ;
    }


    @Override
    public void cancelProcessInstance(Map<String, Object> paramsMap) {
    }


    @Override
    public PageResult<Map<String, Object>> getMyProcessInstancePage(Map<String, Object> paramsMap) {
        return null;
    }

    @Override
    public Map<String, Object> getProcessInstanceVO(String id) {
        return null;
    }



}
